package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Business;
import Database.DatabaseConnector;

@WebServlet("/ClientToGoogleMapServlet")
public class ClientToGoogleServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		DatabaseConnector db = new DatabaseConnector();
		String businessName = (String) request.getSession().getAttribute("username");
		Business business = db.getProfile(businessName);
		String cAddress = (String) request.getParameter("cAddress");
		
		boolean isfirst = true;
		String address = "";
		
		String element = business.getStreetAd();
		if (element != null && !element.isEmpty()) {
			isfirst=false;
			address += element;
		}
		
		element = business.getCity();
		if (element != null && !element.isEmpty()) {
			if (isfirst)
				address += element;
			else
				address += ", " + element;
		}
		
		JSONObject result = new JSONObject();
		try {
			result.put("address", address);
			result.put("cAddress", cAddress);
			response.getWriter().print(result);
			response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
		} catch (JSONException e) {
			e.printStackTrace();
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		} catch(IOException e) {
			e.printStackTrace();
			response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST);
		}
	}
}
